package cn.oveay.classdesign.dao.impl;

import cn.oveay.classdesign.bean.basebean.Recode;
import cn.oveay.classdesign.dao.RecordDao;
import cn.oveay.classdesign.util.jdbc.TxQueryRunner;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.SQLException;
import java.util.List;

public class RecordDaoImpl implements RecordDao {
    private QueryRunner qr = new TxQueryRunner();

    @Override
    public List<Recode> findAll() {
        String sql = "select * from t_record";
        try {
            return qr.query(sql, new BeanListHandler<>(Recode.class));
        } catch (SQLException e) {
            throw new RuntimeException("RecordDao Error. " + e.getMessage());
        }
    }

    @Override
    public List<Recode> findByUserId(String userId) {
        String sql = "select * from t_record where userId=?";
        try {
            return qr.query(sql, new BeanListHandler<>(Recode.class), userId);
        } catch (SQLException e) {
            throw new RuntimeException("RecordDao Error. " + e.getMessage());
        }
    }

    @Override
    public List<Recode> findBySubjectId(String subjectId) {
        String sql = "select * from t_record where subjectId=?";
        try {
            return qr.query(sql, new BeanListHandler<>(Recode.class), subjectId);
        } catch (SQLException e) {
            throw new RuntimeException("RecordDao Error. " + e.getMessage());
        }
    }

    @Override
    public List<Recode> findByOptionId(String optionId) {
        String sql = "select * from t_record where optionId=?";
        try {
            return qr.query(sql, new BeanListHandler<>(Recode.class), optionId);
        } catch (SQLException e) {
            throw new RuntimeException("RecordDao Error. " + e.getMessage());
        }
    }

    @Override
    public List<Recode> findByUserIdANDSubjectId(String userId, String subjectId) {
        String sql = "select * from t_record where subjectId=? and userId=?";
        try {
            return qr.query(sql, new BeanListHandler<>(Recode.class), subjectId, userId);
        } catch (SQLException e) {
            throw new RuntimeException("RecordDao Error. " + e.getMessage());
        }
    }

    @Override
    public Integer findUserIdBySubjectId(String subjectId) {
        String sql = "SELECT distinct userId FROM t_record where subjectId=?";
        try {
            return qr.query(sql, new BeanListHandler<>(String.class), subjectId).size();
        } catch (SQLException e) {
            throw new RuntimeException("RecordDao Error. " + e.getMessage());
        }
    }

    @Override
    public void inseart(Recode recode) {
        String sql = "insert into t_record values(?,?,?,?)";
        Object[] params = {recode.getId(), recode.getSubjectId(), recode.getOptionId(), recode.getUserId()};
        try {
            qr.update(sql, params);
        } catch (SQLException e) {
            throw new RuntimeException("RecordDao Error. " + e.getMessage());
        }
    }
}
